package com.wh.wisdomsite.xbox.client.service.ba;

import com.wh.wisdomsite.xbox.ba.entity.Region;
import com.wh.wisdomsite.xbox.ba.service.IRegionService;
import com.wh.wisdomsite.xbox.common.Constants;
import com.wh.wisdomsite.xbox.common.annotation.NeedInSessionType;
import com.wh.wisdomsite.xbox.common.annotation.ServiceMethod;
import com.wh.wisdomsite.xbox.common.annotation.ServiceProvider;
import com.wh.wisdomsite.xbox.common.reponse.CommonResult;
import com.wh.wisdomsite.xbox.pojo.ba.GetRegionIn;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户端区域controller
 *
 * @version V1.0
 * @Package: com.wh.wisdomsite.xbox.client.service.ba
 * @author: 丁奕
 * @date: 2018-09-19 14:20
 * @Copyright 公司版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2016
 **/
@ServiceProvider()
public class RegionService {

    private static final Log logger = LogFactory.getLog(RegionService.class);

    //省份
    private static final String PROVINCE = "1";
    //城市
    private static final String CITY = "2";
    //区县
    private static final String DISTRICT = "3";

    @Autowired
    private IRegionService iRegionService;

    /**
     * 通过类型和区域id获取省、市、区县集合
     * @param in
     */
    @ServiceMethod(method = "wh.zhgd.ba.region.getList", title = "获取区域列表", version = "1.0", needInSession = NeedInSessionType.NO)
    public CommonResult getList(GetRegionIn in){
        logger.info("获取区域列表->>wh.zhgd.ba.region.getList->>开始");
        CommonResult commonResult = new CommonResult();
        Map<String, Object> resultMap = new HashMap<>();
        List<Map<String,String>> resultList = new ArrayList<>();
        List<Region> regionList = new ArrayList<Region>();
        try{
            if(PROVINCE.equals(in.getType())){
                regionList = iRegionService.getProvinceList(in.getCodeId());
            }
            if(CITY.equals(in.getType())){
                regionList = iRegionService.getCityList(in.getCodeId());
            }
            if(DISTRICT.equals(in.getType())){
                regionList = iRegionService.getDistrictList(in.getCodeId());
            }
            for (Region region : regionList){
                Map<String, String> tempMap = new HashMap<>();
                tempMap.put("codeId", region.getCodeId());
                if(PROVINCE.equals(in.getType())){
                    tempMap.put("regionName", region.getProvince());
                }
                if(CITY.equals(in.getType())){
                    tempMap.put("regionName", region.getCity());
                }
                if(DISTRICT.equals(in.getType())){
                    tempMap.put("regionName", region.getDistrict());
                }
                resultList.add(tempMap);
            }
            commonResult.setCode(Constants.CODE_SUCCESS);
            commonResult.setMessage(Constants.CODE_SUCCESS_MSG);
            commonResult.setResult(resultList);
            logger.info("获取区域列表->>wh.zhgd.ba.region.getList->>结束");
        }catch (Exception e){
            logger.error("获取区域列表->>wh.zhgd.ba.region.getList->>异常：" + e.getMessage());
            commonResult.setCode(Constants.CODE_FAIL);
            commonResult.setMessage(Constants.CODE_FAIL_MSG);
            in.getXboxRequestContext().setException(new Exception(e.getMessage()));
            e.printStackTrace();
        }
        return commonResult;
    }
}
